from api.base_client import BaseClient
from typing import Dict, Any


class AclClient(BaseClient):
    """权限管理客户端"""

    def login(self, auth_key: str, password: str) -> str:
        """用户登录"""
        url = f"{self.account_host}/api/v2/login/enterprise/user_pw"
        data = {
            "authKey": auth_key,
            "passwordx": password
        }

        response = self.http_client.post(url, data, self._get_headers())
        self._handle_api_response(response)

        token = response['data']['_sw_token']
        return f"_sw_token={token}"

    def logout(self, token: str) -> Dict[str, Any]:
        """用户登出"""
        url = f"{self.account_host}/api/v2/logout?token={token}"
        response = self.http_client.get(url, headers=self._get_headers(token))
        return self._handle_api_response(response)

    def get_user_list(self, token: str) -> Dict[str, Any]:
        """获取用户列表"""
        url = self._build_url("/api/user/userList")
        response = self.http_client.get(url, headers=self._get_headers(token))
        return self._handle_api_response(response)

    def create_role(self, token: str, name: str, desc: str) -> Dict[str, Any]:
        """创建角色"""
        url = self._build_url("/api/role/smart-rongmei/save")
        data = {
            "name": name,
            "desc": desc
        }
        response = self.http_client.post(url, data, self._get_headers(token))
        return self._handle_api_response(response)